﻿using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using NP.BPMReportPlatform.Leave.Dto;
using NP.BPMReportPlatform.WordHelper;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Filters;
using MiniSoftware;
using System;
using Volo.Abp.Domain.Entities;
using System.Drawing;
using System.Security.Cryptography;
using DocumentFormat.OpenXml.Bibliography;

namespace NP.BPMReportPlatform.ElectronicSign
{
    /// <summary>
    /// 电子签对外接口
    /// </summary>
    // [Authorize]
    [ApiController]
    [Route("api/esign")]
    public class ESignController : BPMReportPlatformController
    {
        private readonly IESService _eSService;

        public ESignController(IESService eSService)
        {
            _eSService = eSService;
        }

        /// <summary>
        /// 校验用户
        /// </summary>
        /// <param name="worknumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("CheckSignUser")]
        public async Task<string> CheckSignUser(string worknumber)
        {
            return await _eSService.CheckSignUser(worknumber);
        }

        /// <summary>
        /// 查询外部企业(名称)
        /// </summary>
        /// <param name="corporateCode">公司编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("CheckSignCompany")]
        public async Task<CorporateEntity> CheckSignCompany(string corporateCode)
        {
            return await _eSService.CheckSignCompany(corporateCode);
        }

        /// <summary>
        /// 校验所有的法人公司
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("CheckAllCompany")]
        public async Task CheckAllCompany()
        {
            await _eSService.CheckAllCompany();
        }

        /// <summary>
        /// 发起员工证明电子签
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("createEmpCertfication")]

        public async Task<string> CreateEmpCertfication(string instanceNumber)
        {
            return await _eSService.CreateSignFlow(instanceNumber, Dto.ESBusinessEnum.PayProof);
        }

        /// <summary>
        /// 下载签署文档
        /// </summary>
        /// <param name="processId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("download-file")]
        public async Task<string> DownloadContract([FromQuery] string processId)
        {
            return await _eSService.GetESDownloadUrl(processId);
        }

        /// <summary>
        /// 获取签署情况
        /// 流程状态，0草稿，1签署中，2完成，3撤销，4终止，5过期，6删除，7拒签，8作废，9已归档，10预盖章
        /// </summary>
        /// <param name="flowid">流程号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSignDetail")]
        public async Task<string> GetSignDetail(string flowid)
        {
            return await _eSService.GetSignDetail(flowid);
        }

        /// <summary>
        /// 合同模板数据导出测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("ContractTemplate")]
        public async Task<FileResult> ContractTemplateDownload(string outfiletype)
        {
            var values = new Dictionary<string, object>() {
                    {"WNum", "00034564"},
                    {"workNumber", "00034564"},
                    { "CorporateName", "立邦涂料（成都）有限公司" },
                    { "UserName", "盛俐敏" },
                    { "CorporateAddress", "四川省成都经济技术开发区成龙大道600号" },
                    { "CorporatePostCode", "610000" },
                    { "CorporatePhone", "028-88438185" },
                    { "CardNum", "310115198709140414" },
                    { "CardName", "身份证" },
                    { "CardAddress", "杨浦区平凉路2625弄19号302室" },
                    { "ContactAddress", "杨浦区平凉路2625弄19号302室" },
                    { "WorkAddressName", "杨浦区平凉路2625弄19号302室" },
                    { "PostCode", "200090" },
                    { "MobilePhone", "13817968918"},
                    { "TimeSheetType","标准工时"},
                    { "NoFixedImg", new MiniWordPicture() { Path = Environment.CurrentDirectory + "/wwwroot/docs/wordTemplate/icon2.png", Width = 20, Height = 20 } },
                    { "RegularImg", new MiniWordPicture() { Path = Environment.CurrentDirectory + "/wwwroot/docs/wordTemplate/icon1.png", Width = 20, Height = 20 } },
                    { "OtherImg", new MiniWordPicture() { Path = Environment.CurrentDirectory + "/wwwroot/docs/wordTemplate/icon2.png", Width = 20, Height = 20 } },
                    { "NoFixedStartY", " / " },
                    { "NoFixedStartM",  " / " },
                    { "NoFixedStartD",  " / " },
                    { "StartY",  " 2020 " },
                    { "StartM",  " 9 " },
                    { "StartD", " 7 " },
                    { "EndY",  " 2023 " },
                    { "EndM",  " 9 " },
                    { "EndD", " 30 " },
                    { "SYQStartY",  " 2020 " },
                    { "SYQStartM",  " 9 " },
                    { "SYQStartD",  " 7 " },
                    { "SYQEndY",  " 2021 " },
                    { "SYQEndM",  " 3 " },
                    { "SYQEndD",  " 6 " },
                    { "SYQMonth", "6" },
                    { "Position", "专员" },
                    { "ContractSYQY", "2020" },
                    { "ContractSYQM", "9" },
                    { "ContractSYQD", "7" },
                    { "ContractSYQEndY", "2021" },
                    { "ContractSYQEndM", "3" },
                    { "ContractSYQEndD", "6" },
                    { "ContractStartY", "2020" },
                    { "ContractStartM", "7" },
                    { "ContractStartD", "9" },
                    { "NoFixedSYQStartY",  " / " },
                    { "NoFixedSYQStartM",  " / " },
                    { "NoFixedSYQStartD", " / " },
                    { "NoFixedSYQEndY", " / " },
                    { "NoFixedSYQEndM", " / " },
                    { "NoFixedSYQEndD", " / " },
                    { "PartyAName", "立邦涂料（成都）有限公司" },
                    { "PartyAAddress", "四川省成都经济技术开发区成龙大道600号" },
                    { "PartyALegalPerson", "邱洪良" },
                    { "PartyBName","盛俐敏" },
                    { "PartyBAddress", "杨浦区平凉路2625弄19号302室" },
                    { "PartyBCardNo", "310115198709140414" },
                    { "PartyCName", "立邦投资有限公司" },
                    { "PartyCAddress", "上海市浦东新区创业路287号2幢3层" },
                    { "PartyCLegalPerson", "黄守金" },
                    { "NonfarmImg",new MiniWordPicture() { Path = Environment.CurrentDirectory + "/wwwroot/docs/wordTemplate/icon1.png", Width = 20, Height = 20 }},
                    { "FarmingImg",new MiniWordPicture() { Path = Environment.CurrentDirectory + "/wwwroot/docs/wordTemplate/icon2.png", Width = 20, Height = 20 }}};

            values.Add("LegalPerson", "邱洪良");
            values.Add("CreditCode", "2323232323eferere");
            values.Add("School", "上海工程技术大学");
            values.Add("Professional", "计算机科学与技术");
            values.Add("EndDate", "2010-06-30"); 
            values.Add("DepartName", "立邦中国_人力资源总部_数字化运营中心_DHR平台组");

            if (outfiletype == "ContractRenewalTemplate")
            {
                // var rst = MiniWordHelper.CreateWord(WordTemplateEnum.ContractTemplate, values);
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.ContractRenewalTemplate, values);
                string downloadFileName = "ContractRenewalTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "IColorContractTemplate")
            {
                // var rst = MiniWordHelper.CreateWord(WordTemplateEnum.ContractTemplate, values);
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.IColorContractTemplate, values);
                string downloadFileName = "ContractTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "IColorContractRenewalTemplate")
            {
                // var rst = MiniWordHelper.CreateWord(WordTemplateEnum.ContractTemplate, values);
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.IColorContractRenewalTemplate, values);
                string downloadFileName = "ContractTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "ContractTemplate")
            {
                // var rst = MiniWordHelper.CreateWord(WordTemplateEnum.ContractTemplate, values);
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.ContractTemplate, values);
                string downloadFileName = "ContractTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "IUContractTemplate")
            {
                // var rst = MiniWordHelper.CreateWord(WordTemplateEnum.ContractTemplate, values);
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.IUContractTemplate, values);
                string downloadFileName = "IUContractTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "IUContractRenewalTemplate")
            {
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.IUContractRenewalTemplate, values);
                string downloadFileName = "IUContractRenewalTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "ContractChangeTemplate")
            {
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.ContractChangeTemplate, values);
                string downloadFileName = "ContractChangeTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "RetirementContractTemplate")
            {
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.RetirementContractTemplate, values);
                string downloadFileName = "RetirementContractTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "RetirementOutContractTemplate")
            {
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.RetirementOutContractTemplate, values);
                string downloadFileName = "RetirementOutContractTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else if (outfiletype == "InternContractTemplate")
            {
                var pdfM = MiniWordHelper.CreatePdf(WordTemplateEnum.InternContractTemplate, values);
                string downloadFileName = "InternContractTemplate.pdf";
                return File(pdfM, "application/octet-stream", $"{downloadFileName}");
            }
            else
            {
                var rst = MiniWordHelper.CreateWord(WordTemplateEnum.ContractTemplate, values);
                string downloadFileName = "XXX的合同.doc";
                rst.Close();
                return File(rst, "application/octet-stream", $"{downloadFileName}");
            }
        }
    }
}